package com.bdqn.controller;

import com.bdqn.base.Email;
import com.bdqn.base.R;
import com.bdqn.feign.FeignPaymentService;
import com.bdqn.rabbitmq.EmailRabbitMQService;
import com.bdqn.service.OrderService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * OrderController
 *
 * @author LILIBO
 * @since 2023-03-09
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    /**
     * 调用本地服务
     */
    @Resource
    private OrderService orderService;

    /**
     * 调用远程服务
     */
    @Resource
    private FeignPaymentService feignPaymentService;

    /**
     * 调用远程服务
     */
    @Resource
    private EmailRabbitMQService emailRabbitMQService;

    /**
     * 生成订单（调用远程服务完成支付）
     *
     * @param money 订单金额
     * @return
     */
    @PostMapping("/gen")
    public R genOrder(@RequestParam("money") Double money) {
        // 调用本地服务 生成订单
        String orderId = orderService.genOrderId();

        // 调用远程服务 完成订单支付
        R result = feignPaymentService.payByOrderId(orderId, money);
        if (result.getCode() != 0) {
            return R.failure("订单生成失败，请稍后再试~");
        }

        // 订单支付成功，将消息发送到MQ，给用户发送邮件
        Email email = new Email();
        email.setTo("78360159@qq.com");
        email.setContent("【T325-SpringCloud】订单（" +orderId+ "）支付成功！消费金额：" + money);
        emailRabbitMQService.publishMQMail(email); // 将消息发送到消息队列
        return result;
    }
}
